<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Assessment;
use App\Models\Child;
use App\Models\History;
use http\Env\Response;
use Illuminate\Http\Request;

class TrainingController extends BaseController
{

    public function index(Request $request){
        $user = $this->getUser();
        $query = $request->only(['child_name']);
        $query['user_id'] = $user->id;
        $where = History::selectQuery($query);
        $list = History::where($where)->has("child")
            ->orderBy('id', 'desc')
            ->paginate(config('site.other.paginate'));
        $list->appends($query);
        return frontendView('training.index', compact('list', 'query'));
    }

    public function view($id){
        $training = History::find($id);
        if (empty($training)){
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        $user = $this->getUser();
        if (!$user->can("allow", $training->child)){
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }
        //dd($training);
        return frontendView('training.view', compact('training'));
    }

    public function delete($id){
        $training = History::find($id);
        if (empty($training)){
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }
        $user = $this->getUser();
        if (!$user->can("allow", $training->child)){
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }
        if ($training->delete()) {
            return back()->with('success', __('Delete success'));
        }

        return back()->with('error', __('Delete failed'));
    }

    /**
     * 每个学生的训练记录
     *
     * @Date 2020/7/29 上午9:50
     * @param $child_id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function child($child_id){
        $child = Child::find($child_id);
        if (empty($child)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        $user = $this->getUser();
        if (!$user->can('allow', $child)) {
            return back()->with('error', __('Sorry, you are not authorized to access this page.'))->withInput();
        }

        $list = History::where('child_id', $child_id)->orderBy("id", "desc")->paginate(config('site.other.paginate'));


        return frontendView('training.child', compact('list', 'child'));
    }

    public function getchilddata($child_id){
        $child = Child::find($child_id);
        if (empty($child)){
            return response()->json(['status'=>404, 'data'=>[]]);
        }
        $list = Assessment::with("tool")->where('child_id', $child_id)->get();
        if (count($list) == 0){
            return response()->json(['status'=>400, 'data'=>[]]);
        }

        $result = [];
        foreach ($result as $item){
            if (!isset($result[$item['tool_id']])){
                $result[$item['tool_id']]['name'] = $item->tool->name;
            }

            $result[$item['tool_id']]['data'][] = ['x'=>$item->assess_time, 'y'=>$item->agedevelop];
        }
        $list->each(function($item) use (&$result){
            if (!isset($result[$item['tool_id']])){
                $result[$item['tool_id']]['name'] = $item->tool->name;
                $result[$item['tool_id']]['age']['label'] = '生理年龄';
                $result[$item['tool_id']]['agedevelop']['label'] = '发展年龄';
            }

            $result[$item['tool_id']]['age']['data'][] = ['x'=>date('m/d/Y H:i',strtotime($item->assess_time)), 'y'=>floatval($item->age)];
            $result[$item['tool_id']]['agedevelop']['data'][] = ['x'=>date('m/d/Y H:i',strtotime($item->assess_time)), 'y'=>floatval($item->agedevelop)];
        });

        return response()->json(['status'=>200, 'data'=>$result]);

    }
}
